package com.cloudtec.yunzonedep.common.entity;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;

/**
 *   统一返回结果数据格式
 */
@Data
@AllArgsConstructor
@Accessors(chain = true)
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 成功标志
	 */
	private boolean success = true;

	/**
	 * 返回处理消息
	 */
	private String message = "操作成功！";

	/**
	 * 返回代码
	 */
	private Integer code = 0;

	/**
	 * 最新token
	 */
	private String token = "";
	
	/**
	 * 返回数据对象 data
	 */
	private T data;

	public Result() {
		
	}
	
	/**
	 * 时间戳
	 */
	private String timestamp = DateUtil.formatDateTime(new Date());

	public void error500(String message) {
		this.message = message;
		this.code = 500;
		this.success = false;
		this.data=null;
		this.timestamp= DateUtil.formatDateTime(new Date());
	}

	public void success(String message) {
		this.message = message;
		this.code = 200;
		this.success = true;
		this.data=null;
		this.timestamp= DateUtil.formatDateTime(new Date());
	}
	
	public static Result<Object> error(String msg) {
		return error(500, msg);
	}
	
	public static Result<Object> error(int code, String msg) {
		Result<Object> r = new Result<Object>();
		r.setCode(code);
		r.setMessage(msg);
		r.setSuccess(false);
		r.timestamp= DateUtil.formatDateTime(new Date());
		return r;
	}
	
	public static Result<Object> ok() {
		Result<Object> r = new Result<Object>();
		r.setSuccess(true);
		r.setCode(200);
		r.setMessage("操作成功！");
		r.timestamp= DateUtil.formatDateTime(new Date());
		return r;
	}
	
	public static Result<Object> updateOK(String msg) {
		Result<Object> r = new Result<Object>();
		r.setSuccess(true);
		r.setCode(200);
		r.setMessage(msg);
		r.timestamp= DateUtil.formatDateTime(new Date());
		return r;
	}
	
	public static Result<Object> queryOK(Object data) {
		Result<Object> r = new Result<Object>();
		r.setSuccess(true);
		r.setCode(200);
		r.setData(data);
		r.setMessage("查询成功！");
		r.timestamp= DateUtil.formatDateTime(new Date());
		return r;
	}
}